/*
 * @(#)TaxonomyHandler.java 0.0.1-SNAPSHOT
 * Copyright Group B
 */

package grupb.upf.XMLHandlers;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 * <p>This class cointains the functions to parsing the Taxonomy XML elements
 * into Java objects.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public final class TaxonomyHandler extends org.xml.sax.helpers.DefaultHandler {

    /**
     * A new logger <code>grupb.upf.XMLHandlers.TaxonomyHandler</code>
     * is created to add the logs of this Content.
     */
    private static final Log LOG = LogFactory.getLog(TaxonomyHandler.class);
    /**
     * The List of names in a taxonomy.
     */
    private List taxonomy;
    /**
     * The StringBuffer to read the XML elements.
     */
    private StringBuffer accumulator;

    /**
     * This is the constructor of the Handler.
     * @param tax the list taxonomies list
     */
    public TaxonomyHandler(final List tax) {
        this.taxonomy = tax;
    }

    /**
     * This function is called at the beginning and it creates the accumulator.
     */

    @Override
    public void startDocument() {
        accumulator = new StringBuffer();
    }

    /**
     * When the parser encounters plain text (not XML elements),
     * it calls this method, which accumulates the text in a string buffer.
     * Note that this method may be called in multiple times.
     * @param buffer the buffer
     * @param start the initial point
     * @param length the length of the accumulator
     */
    @Override
    public void characters(final char[] buffer, final int start,
            final int length) {
        accumulator.append(buffer, start, length);
    }

    /**
     * At the beginning of each new element, erase any accumulated text.
     * @param namespaceURL the namespaceURL of the element
     * @param localName the name of the element
     * @param qname the qname of the element
     * @param attributes the attributes of the element
     */
    @Override
    public void startElement(final String namespaceURL,
            final String localName, final String qname,
            final Attributes attributes) {
        accumulator.setLength(0);
    }

    /**
     * Take special action when reach the end of selected elements.
     * In this case the accumulated text is stored in the video object.
     * @param namespaceURL the namespaceURL of the element
     * @param localName the name of the element
     * @param qname the qname of the element
     */
    @Override
    public void endElement(final String namespaceURL,
            final String localName, final String qname) {
        if (localName.equals("name")) {
            taxonomy.add(accumulator.toString().trim());
        }
    }

    /**
     * This function is called at the end.
     */
    @Override
    public void endDocument() {
    }

    /**
     * Function to catch warnings.
     * @param exception a SAXParseException
     */
    @Override
    public void warning(final SAXParseException exception) {
        StringBuffer ex = new StringBuffer("Line ")
                .append(exception.getLineNumber())
                .append(": ")
                .append(exception.getMessage());
        LOG.warn(ex);
    }

    /**
     * Function to catch errors.
     * @param exception a SAXParseException
     */
    @Override
    public void error(final SAXParseException exception) {
        StringBuffer ex = new StringBuffer("Line ")
                .append(exception.getLineNumber())
                .append(": ")
                .append(exception.getMessage());
        LOG.error(ex);
    }

    /**
     * Function to catch Fatal Errors.
     * @param exception a SAXParseException
     * @throws SAXException to catch the exception
     */
    @Override
    public void fatalError(final SAXParseException exception)
            throws SAXException {
        StringBuffer ex = new StringBuffer("Line ")
                .append(exception.getLineNumber())
                .append(": ")
                .append(exception.getMessage());
        LOG.fatal(ex);
        throw(exception);
    }
}
